﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Timers;

namespace TimedTask.Tools
{
    /// <summary>
    /// 网络监控
    /// </summary>
    public class NetworkMonitorSM
    {
        public string networkName = "";
        public Action<MonitorInfo> callback = null;

        List<PerformanceCounter> downloadCounters = null;
        List<PerformanceCounter> uploadCounters = null;
        public NetworkMonitorSM()
        {
            downloadCounters = new List<PerformanceCounter>();
            uploadCounters = new List<PerformanceCounter>();
        }

        public void StartMonitor()
        {
            ATimer.setInterval(1000, () =>
            {
                float totalDownlaod = 0;
                float totalUplaod = 0;
                foreach (PerformanceCounter pc in downloadCounters)
                {
                    totalDownlaod += pc.NextValue();
                }
                foreach (PerformanceCounter pc in uploadCounters)
                {
                    totalUplaod += pc.NextValue();
                }

                MonitorInfo mi = new MonitorInfo();
                mi.downloadBytes = totalDownlaod;
                mi.uploadBytes = totalUplaod;
                if (callback != null)
                    callback(mi);
            });
            InitNetCategory();
        }

        public void InitNetCategory()
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");

            foreach (string name in category.GetInstanceNames())
            {
                Console.WriteLine(name);
                if (name == "MS TCP Loopback interface")
                    continue;
                if (!string.IsNullOrEmpty(networkName))
                {
                    if (networkName.Equals(name))
                    {
                        downloadCounters.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", name));
                        uploadCounters.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", name));
                    }
                }
                else
                {
                    downloadCounters.Add(new PerformanceCounter("Network Interface", "Bytes Received/sec", name));
                    uploadCounters.Add(new PerformanceCounter("Network Interface", "Bytes Sent/sec", name));
                }
            }

            if (downloadCounters.Count <= 0)
                Console.WriteLine("Do not Has the Network named with [" + networkName + "]");
        }
    }

    public class MonitorInfo
    {
        public double downloadBytes;
        public double uploadBytes;

        public string DownSpeed { get; set; }
        public string UpSpeed { get; set; }

        public override string ToString()
        {
            int d = (int)(downloadBytes / 1024f);
            int u = (int)(uploadBytes / 1024f);
            DownSpeed = Helper.NetSpeedFormat(downloadBytes);
            UpSpeed = Helper.NetSpeedFormat(uploadBytes);

            //↓ ↡ ↧ ↯ ↴ ↷ ↶ ⇂ ⇃ ⇊ ⇓ ⇕ ⇅ ⇟ ⇣ ⇩ ⍖ ⍗ ☈ ☇ ➘ ￬
            //←↑→↓↔↕↖↗↘↙↚↛↜↝↞↟↠↡↢↣↤↥↦↧↨↩↪↫↬↭↮↯↰↱↲↳↴↵↶↷↸↹↺↻↼↽↾↿⇀⇁⇂⇃  ⍐ ￪ ↑ ↟ ↥ ↾ ↿ ⇞ ⇡ ⍏ ↕ ↨
            //➔➘➙➚➛➜➝➞➟➠➡➢➣➤➥➦➧➨➩➪➫➬➭➮➯➱➲➳➴➵➶➷➸➹➺➻➼➽➾
            return "↓ " + DownSpeed + "  ↑" + UpSpeed;
        }
    }
}
